//
//  ClassifyDetailViewController.m
//  Cartoon
//
//  Created by dllo on 15/10/30.
//  Copyright © 2015年 YSZ. All rights reserved.
//

#import "ClassifyDetailViewController.h"
#import "ClassifyDetailModel.h"
#import "ClassifyDetailTableViewCell.h"
#import "CartoonDetailsViewController.h"
@interface ClassifyDetailViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *arr;
@property (nonatomic,assign) NSInteger num;
@end

@implementation ClassifyDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SelfViewWidth, SelfViewHeight-113) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = NO;
    [self.view addSubview:_tableView];
    [self.tableView registerClass:[ClassifyDetailTableViewCell class] forCellReuseIdentifier:@"Cell"];
    [self handleHeaderRefresh];
    [self handleFooterRefresh];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ClassifyDetailModel *model = [self.arr objectAtIndex:indexPath.row];
    ClassifyDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    cell.classfyDetailModel = model;
    return cell;


}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 110;

}

- (void)handleData
{
    [NetHandler dataWithUrl:[NSString stringWithFormat:@"http://mobilev3.ac.qq.com/Classify/comicClassifyDetailForIos/uin/0/local_version/3.1.0/channel/1001/guest_id/0DB20220-FBA8-4267-AA65-5DC78AFBCEB4/classify_id/%@/page/1/listcnt/20",self.classify_id] complant:^(id data) {
        self.arr = [NSMutableArray array];
        NSArray *dataArr = [data objectForKey:@"data"];
        self.arr = [ClassifyDetailModel modelsWithArr:dataArr];
        [self.tableView reloadData];
        [self.tableView.header endRefreshing];
    }];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    CartoonDetailsViewController *carDetail = [[CartoonDetailsViewController alloc] init];
    
    ClassifyDetailModel *model = [self.arr objectAtIndex:indexPath.row];
    
    carDetail.idtext = model.comic_id;
    
    [self.navigationController pushViewController:carDetail animated:YES];
}


- (void)loadMoreData
{
    
    [NetHandler dataWithUrl:[NSString stringWithFormat:@"http://mobilev3.ac.qq.com/Classify/comicClassifyDetailForIos/uin/0/local_version/3.1.0/channel/1001/guest_id/0DB20220-FBA8-4267-AA65-5DC78AFBCEB4/classify_id/%@/page/%ld/listcnt/20",self.classify_id,_num++] complant:^(id data) {
        NSMutableArray *dataArr = [data objectForKey:@"data"];
        NSMutableArray *moreArr = [ClassifyDetailModel modelsWithArr:dataArr];
        self.arr = (NSMutableArray *)[self.arr arrayByAddingObjectsFromArray:moreArr];
        [_tableView reloadData];
        [self.tableView.footer endRefreshing];
        if (dataArr == nil) {
            NSLog(@"没有数据了");
            [self.tableView.footer endRefreshingWithNoMoreData];
        }
        
        
    }];
    
}


- (void)viewWillAppear:(BOOL)animated
{
    
    _num = 2;
    
}


// 下拉刷新

- (void)handleHeaderRefresh
{
    
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        [self handleData];
    }];
    
    // 马上进入刷新状态
    [self.tableView.header beginRefreshing];
    
}

// 上拉加载

- (void)handleFooterRefresh
{
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        [self loadMoreData];
        
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
